// redPacket/pages/component/openPacket/openPacket.js
const service = require('../../../pages/mine/mineService.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listData: [],
    walletUrl: "",
    redPacketData: null,
    isLoading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ redPacketData: JSON.parse(options.data) })
    this.page = 0
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.openPacket()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoading == true) {
      this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getList: function (e) {
    var self = this;
    var param = { "groupBuyUserId": this.data.redPacketData.groupBuyUserId, startPage: this.page }
    getApp().func.Post(false, getApp().api.groupbuy_list_packet, param,
      function (data) {
        if (data != '') {
          self.page += 1
          self.setData({
            listData: data.items,
            isLoading: data.hasNextPage
          })
        }
      },
      function (error) {
        wx.showToast({
          title: error.data.message,
          icon: 'none',
          duration: 2000,
        })
      })
  },
  openPacket: function (e) {
    var that = this
    // wx.showLoading({
    //   title: '请稍等',
    // })
    var param = { "groupBuyUserId": this.data.redPacketData.groupBuyUserId }
    getApp().func.Post(false, getApp().api.groupbuy_receive_packet, param,
      function (data) {
        // wx.hideLoading()
        that.packetAnimation()
      },
      function (error) {
        // wx.hideLoading()
        wx.showToast({
          title: error.data.message,
          icon: 'none',
          duration: 2000,
        })
        that.packetAnimation()
      })
  },
  packetAnimation: function () {
    var redPacketAnimation = this.selectComponent("#redPacketAnimation");
    redPacketAnimation.separate();
  },
  drawMoneyClick: function (e) {
    wx.redirectTo({
      url: "/withdrawCash/pages/withdrawCash/withdrawCash" + "?title=" + "提现",
    })
  },
  openGroupClick: function (e) {
    wx.redirectTo({
      url: '/redPacket/pages/openGroup/openGroup?userId=' + getApp().globalData.userId
    })
  }

})